<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->char('invite_code', 8)->default('')
                ->comment("邀请码")->after('password');
            $table->unsignedBigInteger('parent_id')->default(0)->comment("邀请人id")->after('id');
            $table->tinyInteger('kyc')->default(0)->comment("身份认证: -1:认证失败 0:未认证 1:申请中 2:认证成功")->after('status');
            $table->unsignedMediumInteger('invite_count')->default(0)->comment("邀请总数")->after('invite_code');
        });

        Schema::create('users_invite', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger("user_id");
            $table->unsignedBigInteger("child_user_id");
            $table->timestamps();
            $table->index("user_id");
        });
        DB::statement("ALTER TABLE `users_invite` COMMENT='用户邀新表'");


        Schema::create('users_kyc', function (Blueprint $table) {
            $table->id();
            $table->char('mobile', 11)->default('')->comment("手机号");
            $table->char('realname', 24)->default('')->comment("真实姓名");
            $table->char('idcard', 24)->default('')->comment("身份证号码");
            $table->string('image_face')->default('')->comment("身份证正面");
            $table->string('image_back')->default('')->comment("身份证反面");

            $table->unsignedBigInteger('user_id');
            $table->timestamps();
        });
        DB::statement("ALTER TABLE `users_kyc` COMMENT='用户认证'");

        Schema::table('loan_application', function (Blueprint $table) {
            $table->timestamp('apply_at')->nullable()
                ->comment("贷款申请时间")->after('new_appid');

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn(['invite_code', 'invite_count', 'parent_id', 'kyc']);
        });

        Schema::dropIfExists('users_invite');
        Schema::dropIfExists('users_kyc');

        Schema::table('loan_application', function (Blueprint $table) {
            $table->dropColumn(['apply_at']);
        });
    }
};
